<?php
//自动生成,请根据需要修改
namespace app\service\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;
/**
* @title 客服会话接口
* @description 客服会话说明
* @group 客服
*/
class ServiceDialog extends Base{

	/**
	* @title 插入记录
	* @description 插入客服会话数据
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/add
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:service_chat_id type:VarChar require:1 default:0 other: desc:服务id
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态(1-正在使用中、0-禁用中)
	* @param name:is_delete type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:in_service type:TinyInt require:1 default:0 other: desc:服务中
	* @param name:last_service_account_id type:Int require:1 default:0 other: desc:最后的客服账号id
	* @param name:merchant_id type:Int require:1 default:0 other: desc:商城id
	* @param name:is_close type:TinyInt require:1 default:0 other: desc:已关闭
	* @param name:close_time type:Int require:1 default:0 other: desc:关闭时间
	* @param name:dialog_type type:VarChar require:1 default:0 other: desc:咨询类型(未知、售前、售后)
	* @param name:service_join_time type:Int require:1 default:0 other: desc:客服加入时间
	* @param name:src_from type:VarChar require:1 default:0 other: desc:来源
	* @param name:service_score type:Int require:1 default:0 other: desc:评分分数
	* @param name:service_comment type:VarChar require:1 default:0 other: desc:评语
	*
	*
	* @return chat_id:窗口id
	* @return service_chat_id:服务id
	* @return status:状态
	* @return is_delete:已删除
	* @return create_time:创建时间
	* @return in_service:服务中
	* @return last_service_account_id:最后的客服账号id
	* @return merchant_id:商城id
	* @return is_close:已关闭
	* @return close_time:关闭时间
	* @return dialog_type:咨询类型
	* @return service_join_time:客服加入时间
	* @return src_from:来源
	* @return service_score:评分分数
	* @return service_comment:评语
	*
	*/
	public function add (){
		//模块 - 输入数据
		$input=input();
		//模块 - 验证器
		$service_dialogValidate = new \app\service\validate\ServiceDialog;
		$validate = $service_dialogValidate->scene('except_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $service_dialogValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 新增  ---
		//插入的数据
		$insert_data = [
			'chat_id'=>$input['chat_id'],
			'service_chat_id'=>$input['service_chat_id'],
			'status'=>$input['status'],
			'is_delete'=>$input['is_delete'],
			'create_time'=>$input['create_time'],
			'in_service'=>$input['in_service'],
			'last_service_account_id'=>$input['last_service_account_id'],
			'merchant_id'=>$input['merchant_id'],
			'is_close'=>$input['is_close'],
			'close_time'=>$input['close_time'],
			'dialog_type'=>$input['dialog_type'],
			'service_join_time'=>$input['service_join_time'],
			'src_from'=>$input['src_from'],
			'service_score'=>$input['service_score'],
			'service_comment'=>$input['service_comment'],
		];
		//添加数据
		$insert_res = Db::name('service_dialog')->data($insert_data)->insert();
		if(!$insert_res){
			$msg['msg'] = '插入数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $insert_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（paginate）
	* @description 获取客服会话的列表数据（分页）
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/get_list
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:service_chat_id type:VarChar require:1 default:0 other: desc:服务id
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态(1-正在使用中、0-禁用中)
	* @param name:is_delete type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:in_service type:TinyInt require:1 default:0 other: desc:服务中
	* @param name:last_service_account_id type:Int require:1 default:0 other: desc:最后的客服账号id
	* @param name:merchant_id type:Int require:1 default:0 other: desc:商城id
	* @param name:is_close type:TinyInt require:1 default:0 other: desc:已关闭
	* @param name:close_time type:Int require:1 default:0 other: desc:关闭时间
	* @param name:dialog_type type:VarChar require:1 default:0 other: desc:咨询类型(未知、售前、售后)
	* @param name:service_join_time type:Int require:1 default:0 other: desc:客服加入时间
	* @param name:src_from type:VarChar require:1 default:0 other: desc:来源
	* @param name:service_score type:Int require:1 default:0 other: desc:评分分数
	* @param name:service_comment type:VarChar require:1 default:0 other: desc:评语
	*
	* @param name:page type:Int require:0 default:0 other: desc:当前页
	* @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
	*
	* @return id:id
	* @return chat_id:窗口id
	* @return service_chat_id:服务id
	* @return status:状态
	* @return is_delete:已删除
	* @return create_time:创建时间
	* @return in_service:服务中
	* @return last_service_account_id:最后的客服账号id
	* @return merchant_id:商城id
	* @return is_close:已关闭
	* @return close_time:关闭时间
	* @return dialog_type:咨询类型
	* @return service_join_time:客服加入时间
	* @return src_from:来源
	* @return service_score:评分分数
	* @return service_comment:评语
	*
	*/
	public function get_list (){
		//模块 - 输入数据
        $input=input();
		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['chat_id','=',$input['chat_id']],
			['service_chat_id','like','%'.$input['service_chat_id'].'%'],
			['status','=',$input['status']],
			['is_delete','=',$input['is_delete']],
			['create_time','=',$input['create_time']],
			['in_service','=',$input['in_service']],
			['last_service_account_id','=',$input['last_service_account_id']],
			['merchant_id','=',$input['merchant_id']],
			['is_close','=',$input['is_close']],
			['close_time','=',$input['close_time']],
			['dialog_type','like','%'.$input['dialog_type'].'%'],
			['service_join_time','=',$input['service_join_time']],
			['src_from','like','%'.$input['src_from'].'%'],
			['service_score','=',$input['service_score']],
			['service_comment','like','%'.$input['service_comment'].'%'],
		];
		//去除为空的条件
		$paginate_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($paginate_map,$v);
		}

		//获取数据
		$select_res = Db::name('service_dialog')->where($paginate_map)->paginate();

		// paginate 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（select）
	* @description 获取客服会话的列表数据（无分页）
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/get_select
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:service_chat_id type:VarChar require:1 default:0 other: desc:服务id
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态(1-正在使用中、0-禁用中)
	* @param name:is_delete type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:in_service type:TinyInt require:1 default:0 other: desc:服务中
	* @param name:last_service_account_id type:Int require:1 default:0 other: desc:最后的客服账号id
	* @param name:merchant_id type:Int require:1 default:0 other: desc:商城id
	* @param name:is_close type:TinyInt require:1 default:0 other: desc:已关闭
	* @param name:close_time type:Int require:1 default:0 other: desc:关闭时间
	* @param name:dialog_type type:VarChar require:1 default:0 other: desc:咨询类型(未知、售前、售后)
	* @param name:service_join_time type:Int require:1 default:0 other: desc:客服加入时间
	* @param name:src_from type:VarChar require:1 default:0 other: desc:来源
	* @param name:service_score type:Int require:1 default:0 other: desc:评分分数
	* @param name:service_comment type:VarChar require:1 default:0 other: desc:评语
	*
	*
	* @return id:id
	* @return chat_id:窗口id
	* @return service_chat_id:服务id
	* @return status:状态
	* @return is_delete:已删除
	* @return create_time:创建时间
	* @return in_service:服务中
	* @return last_service_account_id:最后的客服账号id
	* @return merchant_id:商城id
	* @return is_close:已关闭
	* @return close_time:关闭时间
	* @return dialog_type:咨询类型
	* @return service_join_time:客服加入时间
	* @return src_from:来源
	* @return service_score:评分分数
	* @return service_comment:评语
	*
	*/
	public function get_select (){
		//模块 - 输入数据
        $input=input();
		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['chat_id','=',$input['chat_id']],
			['service_chat_id','like','%'.$input['service_chat_id'].'%'],
			['status','=',$input['status']],
			['is_delete','=',$input['is_delete']],
			['create_time','=',$input['create_time']],
			['in_service','=',$input['in_service']],
			['last_service_account_id','=',$input['last_service_account_id']],
			['merchant_id','=',$input['merchant_id']],
			['is_close','=',$input['is_close']],
			['close_time','=',$input['close_time']],
			['dialog_type','like','%'.$input['dialog_type'].'%'],
			['service_join_time','=',$input['service_join_time']],
			['src_from','like','%'.$input['src_from'].'%'],
			['service_score','=',$input['service_score']],
			['service_comment','like','%'.$input['service_comment'].'%'],
		];
		//去除为空的条件
		$select_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($select_map,$v);
		}

		//获取数据
		$select_res = Db::name('service_dialog')->where($select_map)->select();

		// select 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取单条数据
	* @description 根据ID获取客服会话的指定记录
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');

		// input结果返回
		$msg['data']['id'] = $input['id'];

		//模块 - 验证器
		$service_dialogValidate = new \app\service\validate\ServiceDialog;
		$validate = $service_dialogValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $service_dialogValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('service_dialog')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新客服会话指定记录的数据
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:service_chat_id type:VarChar require:1 default:0 other: desc:服务id
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态(1-正在使用中、0-禁用中)
	* @param name:is_delete type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:in_service type:TinyInt require:1 default:0 other: desc:服务中
	* @param name:last_service_account_id type:Int require:1 default:0 other: desc:最后的客服账号id
	* @param name:merchant_id type:Int require:1 default:0 other: desc:商城id
	* @param name:is_close type:TinyInt require:1 default:0 other: desc:已关闭
	* @param name:close_time type:Int require:1 default:0 other: desc:关闭时间
	* @param name:dialog_type type:VarChar require:1 default:0 other: desc:咨询类型(未知、售前、售后)
	* @param name:service_join_time type:Int require:1 default:0 other: desc:客服加入时间
	* @param name:src_from type:VarChar require:1 default:0 other: desc:来源
	* @param name:service_score type:Int require:1 default:0 other: desc:评分分数
	* @param name:service_comment type:VarChar require:1 default:0 other: desc:评语
	*
	*
	* @return id:id
	* @return chat_id:窗口id
	* @return service_chat_id:服务id
	* @return status:状态
	* @return is_delete:已删除
	* @return create_time:创建时间
	* @return in_service:服务中
	* @return last_service_account_id:最后的客服账号id
	* @return merchant_id:商城id
	* @return is_close:已关闭
	* @return close_time:关闭时间
	* @return dialog_type:咨询类型
	* @return service_join_time:客服加入时间
	* @return src_from:来源
	* @return service_score:评分分数
	* @return service_comment:评语
	*
	*/
	public function edit (){
		//模块 - 输入数据
        $input=input();
		//模块 - 验证器
		$service_dialogValidate = new \app\service\validate\ServiceDialog;
		$validate = $service_dialogValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $service_dialogValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'chat_id'=>$input['chat_id'],
			'service_chat_id'=>$input['service_chat_id'],
			'status'=>$input['status'],
			'is_delete'=>$input['is_delete'],
			'create_time'=>$input['create_time'],
			'in_service'=>$input['in_service'],
			'last_service_account_id'=>$input['last_service_account_id'],
			'merchant_id'=>$input['merchant_id'],
			'is_close'=>$input['is_close'],
			'close_time'=>$input['close_time'],
			'dialog_type'=>$input['dialog_type'],
			'service_join_time'=>$input['service_join_time'],
			'src_from'=>$input['src_from'],
			'service_score'=>$input['service_score'],
			'service_comment'=>$input['service_comment'],
		];
		//获取数据
		$update_res = Db::name('service_dialog')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 按条件删除记录
	* @description 按条件删除客服会话的数据
	* @author 开发者（范浩婷）
	* @url /service/service_dialog/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:service_chat_id type:VarChar require:1 default:0 other: desc:服务id
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态(1-正在使用中、0-禁用中)
	* @param name:is_delete type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:in_service type:TinyInt require:1 default:0 other: desc:服务中
	* @param name:last_service_account_id type:Int require:1 default:0 other: desc:最后的客服账号id
	* @param name:merchant_id type:Int require:1 default:0 other: desc:商城id
	* @param name:is_close type:TinyInt require:1 default:0 other: desc:已关闭
	* @param name:close_time type:Int require:1 default:0 other: desc:关闭时间
	* @param name:dialog_type type:VarChar require:1 default:0 other: desc:咨询类型(未知、售前、售后)
	* @param name:service_join_time type:Int require:1 default:0 other: desc:客服加入时间
	* @param name:src_from type:VarChar require:1 default:0 other: desc:来源
	* @param name:service_score type:Int require:1 default:0 other: desc:评分分数
	* @param name:service_comment type:VarChar require:1 default:0 other: desc:评语
	*
	*
	* @return id:id
	* @return chat_id:窗口id
	* @return service_chat_id:服务id
	* @return status:状态
	* @return is_delete:已删除
	* @return create_time:创建时间
	* @return in_service:服务中
	* @return last_service_account_id:最后的客服账号id
	* @return merchant_id:商城id
	* @return is_close:已关闭
	* @return close_time:关闭时间
	* @return dialog_type:咨询类型
	* @return service_join_time:客服加入时间
	* @return src_from:来源
	* @return service_score:评分分数
	* @return service_comment:评语
	*
	*/
	public function delete (){
		//模块 - 输入数据
        $input=input();
		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['id','=',$input['id']],
			['chat_id','=',$input['chat_id']],
			['service_chat_id','=',$input['service_chat_id']],
			['status','=',$input['status']],
			['is_delete','=',$input['is_delete']],
			['create_time','=',$input['create_time']],
			['in_service','=',$input['in_service']],
			['last_service_account_id','=',$input['last_service_account_id']],
			['merchant_id','=',$input['merchant_id']],
			['is_close','=',$input['is_close']],
			['close_time','=',$input['close_time']],
			['dialog_type','=',$input['dialog_type']],
			['service_join_time','=',$input['service_join_time']],
			['src_from','=',$input['src_from']],
			['service_score','=',$input['service_score']],
			['service_comment','=',$input['service_comment']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('service_dialog')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}

}
